<script setup>
import { ref, reactive, onMounted, computed } from 'vue';
import { ElMessage, ElMessageBox, ElTable, ElTableColumn, ElButton, ElInput, ElForm, ElFormItem, ElDialog, ElSelect, ElOption, ElPagination } from 'element-plus';

// 公海池数据
const publicPoolData = ref([]);

// 搜索条件
const searchForm = reactive({
  opportunityId: '',
  customerName: '',
  source: ''
});

// 分页相关
const currentPage = ref(1);
const pageSize = ref(10);

// 加载公海池数据
const loadPublicPoolData = () => {
  // 模拟API请求，使用静态数据
  publicPoolData.value = [
    {
      id: 4,
      opportunityId: 'OP2001',
      customerName: '深圳电子科技有限公司',
      contactPhone: '13512345678',
      estimatedAmount: 120000,
      status: '进行中',
      probability: '60%',
      expectedCloseDate: '2026-02-28',
      createTime: '2025-10-14 10:30:00',
      updateTime: '2025-10-14 10:30:00'
    },
    {
      id: 5,
      opportunityId: 'OP2002',
      customerName: '杭州互联网公司',
      contactPhone: '13487654321',
      estimatedAmount: 90000,
      status: '进行中',
      probability: '40%',
      expectedCloseDate: '2026-03-15',
      createTime: '2025-10-13 09:15:00',
      updateTime: '2025-10-13 09:15:00'
    },
    {
      id: 6,
      opportunityId: 'OP2003',
      customerName: '成都软件开发公司',
      contactPhone: '13365432198',
      estimatedAmount: 180000,
      status: '进行中',
      probability: '55%',
      expectedCloseDate: '2026-01-30',
      createTime: '2025-10-12 14:45:00',
      updateTime: '2025-10-12 14:45:00'
    }
  ];
};

// 计算总数
const total = computed(() => publicPoolData.value.length);

// 计算当前页显示的数据
const currentPageData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + Number(pageSize.value);
  return publicPoolData.value.slice(start, end);
});

// 搜索
const handleSearch = () => {
  currentPage.value = 1;
};

// 重置搜索条件
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = '';
  });
  currentPage.value = 1;
};

// 处理分页
const handleCurrentChange = (val) => {
  currentPage.value = val;
};

// 处理每页条数变化
const handleSizeChange = (val) => {
  pageSize.value = val;
  currentPage.value = 1;
};

// 认领商机
const claimOpportunity = (row) => {
  ElMessageBox.confirm('确定要认领该商机吗？', '认领确认', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info'
  }).then(() => {
    const index = publicPoolData.value.findIndex(item => item.id === row.id);
    if (index !== -1) {
      publicPoolData.value.splice(index, 1);
      ElMessage.success('认领成功');
    }
  }).catch(() => {
    // 取消认领
  });
};

// 批量认领商机
const batchClaimOpportunity = () => {
  if (publicPoolData.value.length === 0) {
    ElMessage.warning('暂无商机可认领');
    return;
  }
  
  ElMessageBox.confirm('确定要认领全部商机吗？', '批量认领确认', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info'
  }).then(() => {
    publicPoolData.value = [];
    ElMessage.success('批量认领成功');
  }).catch(() => {
    // 取消批量认领
  });
};

// 组件挂载时加载数据
onMounted(() => {
  loadPublicPoolData();
});
</script>

<template>
  <div class="public-pool-container">
    <!-- 搜索区域 -->
    <div class="search-area">
      <el-form :model="searchForm" label-width="80px" inline>
        <el-form-item label="商机ID">
          <el-input v-model="searchForm.opportunityId" placeholder="请输入商机ID" style="width: 150px;"></el-input>
        </el-form-item>
        <el-form-item label="客户名称">
          <el-input v-model="searchForm.customerName" placeholder="请输入客户名称" style="width: 150px;"></el-input>
        </el-form-item>
        <el-form-item label="来源">
          <el-select v-model="searchForm.source" placeholder="请选择来源" style="width: 150px;">
            <el-option label="线索转化" value="clue_conversion"></el-option>
            <el-option label="自主注册" value="self_register"></el-option>
            <el-option label="合作伙伴" value="partner"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
          <el-button type="success" @click="batchClaimOpportunity">批量认领</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="table-area">
      <el-table :data="currentPageData" stripe style="width: 100%">
        <el-table-column prop="opportunityId" label="商机ID" width="120"></el-table-column>
        <el-table-column prop="customerName" label="客户名称" width="180"></el-table-column>
        <el-table-column prop="contactPhone" label="联系电话" width="150"></el-table-column>
        <el-table-column prop="estimatedAmount" label="预计金额" width="120">
          <template #default="{ row }">
            {{ row.estimatedAmount.toLocaleString() }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100"></el-table-column>
        <el-table-column prop="probability" label="成交概率" width="100"></el-table-column>
        <el-table-column prop="expectedCloseDate" label="预计成交日期" width="150"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" @click="claimOpportunity(row)">认领</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="pagination-area">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      ></el-pagination>
    </div>
  </div>
</template>

<style scoped>
.public-pool-container {
  padding: 20px;
}

.search-area {
  background-color: #f5f7fa;
  padding: 15px;
  border-radius: 4px;
  margin-bottom: 20px;
}

.table-area {
  background-color: #fff;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination-area {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-button + .el-button {
  margin-left: 10px;
}
</style>